package com.basketball.gym.utils;

import javax.xml.crypto.Data;
import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class DateUtils {
    /**
     * 将日期对象转换为指定的日期字符串
     *
     * @param date   传入的日期对象
     * @param format 格式
     * @return 日期字符串
     */
    public static String dateToString(Date date, String format) {
        String string = "";
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(format);
        if (date != null) {
            string = simpleDateFormat.format(date);
        }
        return string;
    }

    /**
     * 将日期字符串转换为一个日期对象
     *
     * @param datestr 日期字符串
     * @param format  格式
     * @return Date
     * @throws ParseException
     */
    public static Date stingToDate(String datestr, String format) throws ParseException {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(format);

        return simpleDateFormat.parse(datestr);
    }

    /**
     * 获得当日日期的Timestamp
     */
    public static Timestamp getTodayTimestamp() throws ParseException {
        Date now = new Date();
        String todayString = DateUtils.dateToString(now, "yyyy-MM-dd");
        return new Timestamp(DateUtils.stingToDate(todayString, "yyyy-MM-dd").getTime());
    }

    /**
     * 获取当前日期的Date
     */
    public static Date getTodayDate() throws ParseException {
        Date now = new Date();
        String todayString = DateUtils.dateToString(now, "yyyy-MM-dd");
        return DateUtils.stingToDate(todayString, "yyyy-MM-dd");
    }

    /**
     * 获取12小时后的时间
     */
    public static Date getAfterHalfDayTime(){
        //使用roll方法进行向前回滚
        //cl.roll(Calendar.DATE, -1);
        //使用set方法直接进行设置
        Calendar calendar=Calendar.getInstance();
        calendar.set(Calendar.HOUR_OF_DAY, calendar.get(Calendar.HOUR_OF_DAY) + 12);
        return calendar.getTime();
    }

    /**
     * 根据timeId获取时间
     */
    public static Date getTimeIdTime(Date date,int timeId){
        Calendar calendar=Calendar.getInstance();
        calendar.setTime(date);
        calendar.set(Calendar.HOUR_OF_DAY, calendar.get(Calendar.HOUR_OF_DAY) + 10+timeId);
        return calendar.getTime();
    }

    public static void main(String[] args) {
        System.out.println(getAfterHalfDayTime());;
    }
}
